package br.com.winesys.geta.databinding.component.table;

import java.text.NumberFormat;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import br.com.winesys.geta.databinding.component.editor.BigDecimalEditor;
import br.com.winesys.geta.databinding.component.editor.DoubleEditor;
import br.com.winesys.geta.databinding.component.editor.FloatEditor;
import br.com.winesys.geta.databinding.component.editor.IntegerEditor;
import br.com.winesys.geta.databinding.component.editor.LimitedTextField;
import br.com.winesys.geta.databinding.component.editor.LongEditor;
import br.com.winesys.geta.databinding.component.table.editors.FrameworkCellEditor;
import br.com.winesys.geta.databinding.component.table.renderers.BooleanTableCellRenderer;
import br.com.winesys.geta.databinding.component.table.renderers.DateTableCellRenderer;
import br.com.winesys.geta.databinding.component.table.renderers.NoBorderTableCellRenderer;
import br.com.winesys.geta.databinding.component.table.renderers.NumberTableCellRenderer;
import com.toedter.calendar.JDateChooser;

public final class ColumnFactory {

  private ColumnFactory() {
  }

  public static Column createStringColumn(String property, String name, int width) {
    return new Column(property, name, width, true, new NoBorderTableCellRenderer(), new FrameworkCellEditor(new JTextField()));
  }

  public static Column createLimitedStringColumn(String property, String name, int width, int limit) {
    return new Column(property, name, width, true, new NoBorderTableCellRenderer(), new FrameworkCellEditor(new LimitedTextField(limit)));  }

  public static Column createFormattedStringColumn(String property, String name, int width, String mask) {
    try {
      return new Column(property, name, width, true, new NoBorderTableCellRenderer(), new FrameworkCellEditor(new JFormattedTextField(new MaskFormatter(mask))));
    } catch (Exception ex) {
      throw new RuntimeException(String.format("Unable to create formatted text field: %s", mask));
    }
  }

  public static Column createReadOnlyStringColumn(String property, String name, int width) {
    return new Column(property, name, width, false, new NoBorderTableCellRenderer(), new FrameworkCellEditor(new JTextField()));
  }

  public static Column createBooleanColumn(String property, String name, int width) {
    return new Column(property, name, width, true, new BooleanTableCellRenderer(), new DefaultCellEditor(new JCheckBox()));
  }

  public static Column createReadOnlyBooleanColumn(String property, String name, int width) {
    return new Column(property, name, width, false, new BooleanTableCellRenderer(), new DefaultCellEditor(new JCheckBox()));
  }

  public static Column createDateColumn(String property, String name, int width, String format) {
    return new Column(property, name, width, true, new DateTableCellRenderer(format), new FrameworkCellEditor(new JDateChooser(format, format, '_')));
  }

  public static Column createReadOnlyDateColumn(String property, String name, int width, String format) {
    return new Column(property, name, width, false, new DateTableCellRenderer(format), new FrameworkCellEditor(new JDateChooser(format, format, '_')));
  }

  public static Column createIntegerColumn(String property, String name, int width) {
    return new Column(property, name, width, true, new NumberTableCellRenderer("#,##0"), new FrameworkCellEditor(new IntegerEditor()));
  }
  
  public static Column createIntegerColumn(String property, String name, int width, String format) {
	    return new Column(property, name, width, true, new NumberTableCellRenderer(format), new FrameworkCellEditor(new IntegerEditor()));
	  }

  public static Column createReadOnlyIntegerColumn(String property, String name, int width) {
    return new Column(property, name, width, false, new NumberTableCellRenderer("#,##0"), new FrameworkCellEditor(new IntegerEditor()));
  }

  public static Column createReadOnlyIntegerColumn(String property, String name, int width, String format) {
	 return new Column(property, name, width, false, new NumberTableCellRenderer(format), new FrameworkCellEditor(new IntegerEditor()));
  }

  public static Column createLongColumn(String property, String name, int width) {
    return new Column(property, name, width, true, new NumberTableCellRenderer("#,##0"), new FrameworkCellEditor(new LongEditor()));
  }
  
  public static Column createLongColumn(String property, String name, int width, String format) {
	    return new Column(property, name, width, true, new NumberTableCellRenderer(format), new FrameworkCellEditor(new LongEditor()));
  }

  public static Column createReadOnlyLongColumn(String property, String name, int width) {
    return new Column(property, name, width, false, new NumberTableCellRenderer("#,##0"), new FrameworkCellEditor(new LongEditor()));
  }
  
  public static Column createReadOnlyLongColumn(String property, String name, int width, String format) {
	    return new Column(property, name, width, false, new NumberTableCellRenderer(format), new FrameworkCellEditor(new LongEditor()));
  }

  public static Column createFloatColumn(String property, String name, int width, NumberFormat format) {
    return new Column(property, name, width, true, new NumberTableCellRenderer(format), new FrameworkCellEditor(new FloatEditor(format)));
  }

  public static Column createFloatColumn(String property, String name, int width, String format) {
    return new Column(property, name, width, true, new NumberTableCellRenderer(format), new FrameworkCellEditor(new FloatEditor(format)));
  }

  public static Column createReadOnlyFloatColumn(String property, String name, int width, NumberFormat format) {
    return new Column(property, name, width, false, new NumberTableCellRenderer(format), new FrameworkCellEditor(new FloatEditor(format)));
  }

  public static Column createReadOnlyFloatColumn(String property, String name, int width, String format) {
    return new Column(property, name, width, false, new NumberTableCellRenderer(format), new FrameworkCellEditor(new FloatEditor(format)));
  }

  public static Column createDoubleColumn(String property, String name, int width, NumberFormat format) {
    return new Column(property, name, width, true, new NumberTableCellRenderer(format), new FrameworkCellEditor(new DoubleEditor(format)));
  }

  public static Column createDoubleColumn(String property, String name, int width, String format) {
    return new Column(property, name, width, true, new NumberTableCellRenderer(format), new FrameworkCellEditor(new DoubleEditor(format)));
  }

  public static Column createReadOnlyDoubleColumn(String property, String name, int width, NumberFormat format) {
    return new Column(property, name, width, false, new NumberTableCellRenderer(format), new FrameworkCellEditor(new DoubleEditor(format)));
  }

  public static Column createReadOnlyDoubleColumn(String property, String name, int width, String format) {
    return new Column(property, name, width, false, new NumberTableCellRenderer(format), new FrameworkCellEditor(new DoubleEditor(format)));
  }

  public static Column createBigDecimalColumn(String property, String name, int width, NumberFormat format) {
    return new Column(property, name, width, true, new NumberTableCellRenderer(format), new FrameworkCellEditor(new BigDecimalEditor(format)));
  }

  public static Column createBigDecimalColumn(String property, String name, int width, String format) {
    return new Column(property, name, width, true, new NumberTableCellRenderer(format), new FrameworkCellEditor(new BigDecimalEditor(format)));
  }

  public static Column createReadOnlyBigDecimalColumn(String property, String name, int width, NumberFormat format) {
    return new Column(property, name, width, false, new NumberTableCellRenderer(format), new FrameworkCellEditor(new BigDecimalEditor(format)));
  }

  public static Column createReadOnlyBigDecimalColumn(String property, String name, int width, String format) {
    return new Column(property, name, width, false, new NumberTableCellRenderer(format), new FrameworkCellEditor(new BigDecimalEditor(format)));
  }
}